package com.caiyi.financial.nirvana.sms.gateway.sgip;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.gateway.KeepConnectGateWay;
import com.caiyi.financial.nirvana.sms.gateway.cmpp.CMPPMessageTool;
import com.caiyi.financial.nirvana.sms.gateway.cmpp.LongMsgHeader;
import com.caiyi.financial.nirvana.sms.gateway.sgip.message.SGIPMessage;
import com.caiyi.financial.nirvana.sms.gateway.sgip.message.body.SGIPSubmit;
import com.caiyi.financial.nirvana.sms.queue.SendQueueEntity;
import com.caiyi.financial.nirvana.sms.util.CommUtil;
import com.caiyi.financial.nirvana.sms.util.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class SGIPClient extends KeepConnectGateWay {

    private static final Logger logger = LoggerFactory.getLogger(SGIPClient.class);

    public SGIPClient() {
        super();
    }

    public void start() throws InterruptedException {
        super.start();
        bootstrap.handler(new SGIPClientInitializer(this));
    }

    //发送短信
    @Override
    public void sendMessage(SendQueueEntity entity) {
        logger.info("SGIPClient send start : {}", JSON.toJSONString(entity));
        SGIPMessage message = new SGIPMessage();
        SGIPHeader header = new SGIPHeader();
        header.setCommandID(SGIPMessage.SGIP_SUBMIT);
        header.setTimestamp(CMPPMessageTool.getMMDDHHMMSS());
        message.setHeader(header);
        SGIPSubmit submit = new SGIPSubmit();
        submit.setSpNumber(entity.getAccessNumber());
        submit.setUserNumber(new String[]{entity.getPhone()});
        submit.setCorpId(info.getPassword().substring(1));
        submit.setServiceType(info.getNotifyResult());
        submit.setMessageContent(entity.getContent());
        // 长短信内容分割
        List<String> contentChips = CommUtil.splitLongContent(entity.getContent());
        int sendNum = contentChips.size() > 1 ? contentChips.size() : 1;
        // 长短信设置
        if (sendNum > 1) {
            byte total = (byte) sendNum;
            byte serial = CMPPMessageTool.getOne();
            for (int index = 0; index < contentChips.size(); index++) {
                SGIPMessage message2 = (SGIPMessage) message.clone();
                SGIPSubmit submit2 = (SGIPSubmit) submit.clone();
                LongMsgHeader msgHeader = new LongMsgHeader();
                // 设置sequenceId对应的发送信息
                int sequenceId = Constant.seqNo.incrementAndGet();
                this.S2M.put(String.valueOf(sequenceId), entity.getMessageId());
                message2.setSequenceId(sequenceId);
                msgHeader.setSerial(serial);
                msgHeader.setTotal(total);
                msgHeader.setNumber((byte) (index + 1));
                String contentChip = contentChips.get(index);
                submit2.setMessageContent(contentChip);
                submit2.setMsgHeader(msgHeader);
                submit2.setTpUdhi((byte) 1);
                message2.setBody(submit2);
                context.writeAndFlush(message2).addListener((future) -> logger.info("SGIPClient phone : {}, isDone : {}, isSuccess : {}", entity.getPhone(), future.isDone(), future.isSuccess()));
            }
        } else {
            int sequenceId = Constant.seqNo.incrementAndGet();
            this.S2M.put(String.valueOf(sequenceId), entity.getMessageId());
            message.setSequenceId(sequenceId);
            message.setBody(submit);
            context.writeAndFlush(message).addListener((future) -> logger.info("SGIPClient phone : {}, isDone : {}, isSuccess : {}", entity.getPhone(), future.isDone(), future.isSuccess()));
        }
    }

}
